package com.itheima.controller;

import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.SysMenuDto;
import com.itheima.entity.SysMenu;
import com.itheima.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/allData")
    public R<List<SysMenuDto>> allData(){
        List<SysMenuDto> allMenus = this.getAllMenus();
        return R.success(allMenus);

    }
    public List<SysMenuDto> getChild(Integer id, List<SysMenuDto> menus) {
        //二级菜单列表
        List<SysMenuDto> childList = new ArrayList<>();
        //遍历所有的菜单的Pid 和根菜单的id比较  菜单的Pid与根菜单的id相同就是二级菜单
        for (SysMenuDto menu : menus) {
            if (id.equals(menu.getPid())) {
                childList.add(menu);
            }
        }

        //遍历 获取二级菜单的子菜单
        for (SysMenuDto menu : childList) {
            List<SysMenuDto> child = getChild(menu.getId(),menus);
            menu.setChildren(child);
        }
        //递归出口  childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    public List<SysMenuDto> getAllMenus(){
        List<SysMenu> menus = sysMenuService.list();
        //创建一个带有子菜单的SysMenuDto集合,用来封装数据
        List<SysMenuDto> menuDtos = new ArrayList<>();
        for (SysMenu menu : menus) {
            SysMenuDto menuDto = new SysMenuDto();
            //遍历拿到的菜单数据并复制到menuDto中
            BeanUtils.copyProperties(menu,menuDto);
            menuDtos.add(menuDto);
        }
        //根目录菜单
        List<SysMenuDto> rootMenus = new ArrayList<>();
        for (SysMenu menu : menus) {
            //pid（上级Id）为0的是根菜单
            if (0==(menu.getPid())) {
                SysMenuDto menuDto = new SysMenuDto();
                //复制根菜单的数据到menuDto中
                BeanUtils.copyProperties(menu,menuDto);
                rootMenus.add(menuDto);
            }
        }
        //遍历根菜单，找到二级菜单（根菜单的id和所有菜单中的pid比较）
        for (SysMenuDto rootMenu : rootMenus) {
            List<SysMenuDto> child = getChild(rootMenu.getId(), menuDtos);
            rootMenu.setChildren(child);
        }
        return rootMenus;
    }

    /**
     * 添加新的菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMenu sysMenu){
        if (sysMenu==null){
            throw new CustomException("当前无任何数据,请检查");
        }
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return R.success("添加成功");
    }

    /**
     * 菜单数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<SysMenu> getForId(String id){
        SysMenu sysMenu = sysMenuService.getById(id);
        if (sysMenu==null){
            throw new CustomException("无当前数据");
        }
        return R.success(sysMenu);
    }

    /**
     * 菜单数据修改
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return R.success("修改成功");
    }

    /**
     * 菜单数据删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(String id){
        sysMenuService.removeById(id);
        return R.success("删除成功");
    }

}
